package com.suep.business.controller;

import com.suep.business.domain.Order;
import com.suep.business.domain.OrderItem;
import com.suep.business.dto.OrderDTO;
import com.suep.business.dto.request.OrderRequest;
import com.suep.business.service.IOrderService;
import com.suep.system.config.BaseR;
import com.suep.system.utils.RequestAttributeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    /**
     * 执行订单生成逻辑
     * */
    @PostMapping("/pay")
    public BaseR<String> pay(@RequestBody OrderRequest orderRequest,HttpServletRequest request){
        Order order=orderRequest.getOrder();
        List<OrderItem> orderItemList=orderRequest.getOrderItemList();
        Integer userId= RequestAttributeUtil.getUserIdFromRequest(request);
        orderService.pay(order,orderItemList,userId);
        return new BaseR<>().ok("支付成功,请耐心等待订单完成");
    }
    /**
     * 用户端实时监控订单状态
     * */
    @GetMapping("/monitor")
    public BaseR<List<Order>> monitor(HttpServletRequest request){
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("监控订单状态中",orderService.selectOrderInProgress(userId));
    }

    /**
     * 商家查询今日订单
     * */
    @GetMapping("/getByStoreIdByToday")
    public BaseR<List<OrderDTO>> getOrderByStoreIdByToday(HttpServletRequest request){
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("查询今日订单成功",orderService.selectOrdersByStoreIdByToday(userId));
    }

    /**
     * 用户查询历史订单
     * */

    public BaseR<List<OrderDTO>> getOrderListByUserId(HttpServletRequest request){
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("获取历史订单成功",orderService.selectOrderDTOListByUserId(userId));

    }

    /**
     * 订单切换到下一个状态
     * */
    @PutMapping("/nextStatus")
    public BaseR nextOrderStatus(@RequestParam("orderId") Long orderId){
        orderService.updateNextStatus(orderId);
        return new BaseR<>().ok("订单成功转换状态");
    }



    @GetMapping("/getById")
    public BaseR<Order> getById(@RequestParam Long orderId) {
        return new BaseR<>().ok("查询成功", orderService.selectOrderById(orderId));
    }

    @GetMapping("/getByUserId")
    public BaseR<List<Order>> getByUserId(@RequestParam Integer userId) {
        return new BaseR<>().ok("查询成功", orderService.selectOrdersByUserId(userId));
    }

    @GetMapping("/getByStoreIdByToday1")
    public BaseR<List<Order>> getByStoreIdByToday(HttpServletRequest request) {
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("查询成功", orderService.selectOrdersByStoreId(userId));
    }
    @GetMapping("/getByStoreId")
    public BaseR<List<Order>> getByStoreId(HttpServletRequest request) {
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("查询成功", orderService.selectOrdersByStoreId(userId));
    }


    @GetMapping("/getByDeliveryManId")
    public BaseR<List<Order>> getByDeliveryManId(@RequestParam Integer deliveryManId) {
        return new BaseR<>().ok("查询成功", orderService.selectOrdersByDeliveryManId(deliveryManId));
    }

    @PostMapping("/add")
    public BaseR<Integer> add(@RequestBody Order order) {
        return new BaseR<>().ok("插入成功", orderService.insertOrder(order));
    }

    @PutMapping("/change")
    public BaseR<Integer> change(@RequestBody Order order) {
        return new BaseR<>().ok("更新成功", orderService.updateOrder(order));
    }

    @DeleteMapping("/removeById")
    public BaseR<Integer> removeById(@RequestParam Integer orderId) {
        return new BaseR<>().ok("删除成功", orderService.deleteOrderById(orderId));
    }
}
